Output Devices

Goal

Make a device that I can play sounds with and if there is enough time make the led on the rp2040 react to the sounds that are played

Materials and software

  1. Fusion 360
  2. Eagle for circuit design (in Fusion)
  3. Circuit board bill
  4. Speaker
  5. Xiao RP2040
  6. Female and male jumper cables and pins
  7. LM4871 amplifier
  8. Capacitor bible
  9. Resistors

Fusion Work

  1. The datasheet this time was a lot more confusing than the last one, so needed to ask for help in reading it
  2. Having a diagram was very useful in thinking about how the board would be laid out
  3. I also learned that sometimes things do not matter how theyre connected if theyre part of the same like series. I was very worried things had to go in certain ways and made drawing traces hard until I was told they just all have to be told what line they are on
  4. There is a function check drc that will tell you if you're within proper paramaers which helped me figure out how to solve skethes too close to eachother. I had to go back to fusion after putting sketch on mill cause it was throwing an error.
  5. I did not learn until later that I should have made my traces thicker
diagram
Paper diagram
board
Updated board

Circuit board mill

  1. Many tragedies were commited with milling such as cutting myself, scratching off the traces by an accident, using a terrible bit, etc
  2. I actually had to mill three boards because I messed up the first two
  3. I have gotten much quicker to mill though flow wise which I was very happy about

Soldering

  1. I learned that you should really make sure you have the right parts on the board in the right place because I needed to desolder multiple things and it was an awful time
  2. I also learned that esp rp2040 also have micropython which I did not realize cause I desldered it and put a xiao to replace it.
  3. I need to be more patient when desoldering because i broke multiple traces in the process.
  4. Learned the beauty of solder paste and the capacity/resistor bibles
  5. bible
    Bible
    tracm
    Need to not forget
    burned
    Jerry rigged test board

Analyzing

  1. After checking connections and looking for shorts or disconnected there was still one trace that was broken which i used a wire to complete it
  2. Tried checking the signal and everyhing seemed to be working fine, but there was still no sound
  3. After that there was still no volume being out out, Anthony eventually helped me realize that the amplifier connected defaulted to the shutdown pin to be set high when we needed low
  4. The video include simple sound playing with an led and then a more complex sound playing with an led reacting to the sounds being played
  5. Big lesson learned is that there are libraries written in C to convert sound a lot easier than micropython which I could not figure out
  6. final
    Final Board
        
    import machine
    import utime
    from ws2812 import WS2812
    
    # Define the pin connected to the LM4871 amplifier
    signal_pin = machine.Pin(3)
    shutdown_pin = machine.Pin(4, machine.Pin.OUT)
    
    # Create a PWM object
    pwm = machine.PWM(signal_pin)
    shutdown_pin.low()
    
    # Function to play a continuous tone at a given frequency, volume, and duty cycle
    def play_tone(frequency, volume, duty_cycle, duration):
        pwm.freq(int(frequency))
        pwm.duty_u16(int(duty_cycle * volume))
        utime.sleep_ms(duration)
        pwm.duty_u16(0)
    
    # LED Configuration
    BLACK = (0, 0, 0)
    BLUE = (0, 0, 255)
    PURPLE = (80, 0, 80)
    CYAN = (0, 255, 255)
    WHITE = (255, 255, 255)
    YELLOW = (255, 255, 0)
    ORANGE = (255, 165, 0)
    RED = (255, 0, 0)
    COLORS = (BLACK, BLUE, PURPLE, CYAN, WHITE, YELLOW, ORANGE, RED)
    
    led = WS2812(12, 1)  # WS2812(pin_num, led_count)
    
    # Note frequencies (Hz) for a custom melody
    notes = {'C4': 261.63, 'D4': 293.66, 'E4': 329.63, 'F4': 349.23, 'G4': 392.00, 'A4': 440.00, 'B4': 493.88}
    
    # Colors corresponding to each note
    note_colors = {'C4': BLUE, 'D4': PURPLE, 'E4': CYAN,
                   'F4': WHITE, 'G4': YELLOW, 'A4': ORANGE, 'B4': RED}
    
    # Custom melody with varied volume, speed, and mood
    melody = [
        ('C4', 0.8, 100, 300), ('D4', 0.6, 80, 200), ('E4', 0.8, 100, 300), ('G4', 0.6, 80, 200),
        ('C4', 0.7, 90, 250), ('D4', 0.5, 70, 180), ('E4', 0.7, 90, 250), ('G4', 0.5, 70, 180),
        ('F4', 0.8, 100, 300), ('E4', 0.6, 80, 200), ('D4', 0.8, 100, 300), ('C4', 0.6, 80, 200),
        ('F4', 0.7, 90, 250), ('E4', 0.5, 70, 180), ('D4', 0.7, 90, 250), ('C4', 0.5, 70, 180),
        ('G4', 0.8, 100, 300), ('A4', 0.6, 80, 200), ('B4', 0.8, 100, 300), ('G4', 0.6, 80, 200),
        ('G4', 0.7, 90, 250), ('A4', 0.5, 70, 180), ('B4', 0.7, 90, 250), ('G4', 0.5, 70, 180)
    ]
    
    # Repeat the melody
    repeat_count = 3  # Adjust the number of repetitions as needed
    
    for _ in range(repeat_count):
        for note, volume, duty_cycle, duration in melody:
            frequency = notes[note]
            play_tone(frequency, volume, duty_cycle, duration)
            led.pixels_fill(note_colors[note])
            led.pixels_show()
            utime.sleep_ms(duration)
            led.pixels_fill(BLACK)
            led.pixels_show()
    
        # Add a small delay between repetitions
        utime.sleep_ms(500)